<template>
	<view class="main">
		<view class="pageTop">
			<navbar ref="navbar" :config="config"></navbar>

			<view class="dataCls">
				<view class="dataCls_l">
					<view class="dataCls_l_num">{{loveData.love_value}}</view>
					<view>爱心数（颗）</view>
					<view>累计爱心值：{{loveData.cumulative_love_value}}</view>
				</view>
				<view class="dataCls_r">
					<view @click="open()">兑换公益积分</view>
				</view>
			</view>

			<view class="shadeBox"></view>
		</view>

		<view class="listBox">
			<view class="listBox_title">
				<view>
					<view class="listBox_line"></view>
					<view class="listBox_circle"></view>
				</view>
				<view class="listBox_title_text">爱心明细</view>
				<view>
					<view class="listBox_circle"></view>
					<view class="listBox_line2"></view>
				</view>
			</view>

			<view class="list_item" v-for="(v,i) in list" :key="i">
				<view class="list_item_l">
					<view class="list_item_l_text">{{v.remark}}</view>
					<view class="list_item_l_time">{{v.create_time.substr(0,10)}}</view>
				</view>
				<view class="list_item_r c_green" :class="{c_red:!v.bill_type}">{{v.bill_type?'+':'-'}} {{v.value}}
				</view>
			</view>

			<view class="loadmoreBox">
				<u-loadmore :status="status" />
			</view>
		</view>

		<!-- 兑换弹窗 -->
		<u-popup v-model="show" mode="center" border-radius="10">
			<view class="popBox">
				<view class="popBox_title">请确认兑换额度</view>
				<view class="popBox_center">
					<view class="popBox_inpBox">
						<view class="popBox_inp">
							<input type="text" @input="intNum" v-model="loveValue">
						</view>
						<view class="popBox_inp_all" @click="inpAll">全部</view>
					</view>
					<view class="popBox_tig">规则：当前兑换比例为{{pointsConfig.love_value}}:{{pointsConfig.public_welfare_integral}}</view>
					<view class="popBox_btnBox">
						<view class="popBox_btnBox_close" @click="close">关闭</view>
						<view class="popBox_btnBox_confirm" @click="loveValueExchange()">确认兑换</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					back: true, //false是tolbar页面 是则不写
					title: '我的爱心',
					color: '#ffffff',
					//背景颜色;参数一：透明度（0-1）;参数二：背景颜色（array则为线性渐变，string为单色背景）
					backgroundColor: [0, 'transparent'],
					statusBarFontColor: '#ffffff',
					rightSlot: true,
				},
				status: "nomore",
				show: false,
				totalPage: 0,
				list: [],
				param: {
					page: 1,
					limit: 10
				},
				loveValue: "",
				loveData:{},
				pointsConfig:{}
			}
		},
		onLoad() {
			this.getList();
			this.getData();
			this.getPointsConfig()
		},
		onPullDownRefresh() {
			this.getList();
			this.getData()
		},
		onReachBottom() {
			if (this.status == "loadmore") {
				this.param.page++
			}
			this.getList();
		},
		methods: {
			open() {
				this.show = true;
				this.loveValue = "";
			},
			close() {
				this.show = false;
				this.loveValue = "";
			},
			getData() {
				this.$http.get('/account/app-account-info').then(res => {
					if (res && res.code == 200) {
						this.loveData=res.data
					}
				})
			},
			// 只允许输入整数
			intNum(e){
				this.$nextTick(()=>{
					this.loveValue=parseInt(e.detail.value)||''
				})
			},
			// 获取比例规则
			getPointsConfig(){
				this.$http.get('/points/exchange/config/info').then(res=>{
					if(res&&res.code==200){
						this.pointsConfig=res.data
					}
				})
			},
			// 输入全部积分
			inpAll(){
				this.loveValue=this.loveData.love_value
			},
			// 爱心明细
			getList() {
				this.status = "loading"
				this.param.page=1;
				this.$http.get('/user/love/record/page', this.param).then(res => {
					if (res && res.code == 200) {
						this.list = res.page.list;
						this.totalPage = res.page.totalPage;
						if (this.totalPage > this.list.length) {
							this.status = "loadmore"
						} else {
							this.status = "nomore"
						}
						uni.stopPullDownRefresh()
					}
				})
			},
			// 兑换
			loveValueExchange() {
				if(!this.loveValue){
					uni.showToast({
						icon:"none",
						title: '请输入兑换爱心值！',
						duration: 2000
					});
					return false
				}
				this.$http.post(`/user/love/record/loveValueExchange/${this.loveValue}`).then((res => {
					if (res && res.code == 200) {
						uni.showToast({
							title: '兑换成功！',
							duration: 2000
						});
						this.close()
						this.getList()
						this.getData()
					}
				}))
			}
		}
	}
</script>

<style lang="scss">
	.pageTop {
		width: 100%;
		height: 450rpx;
		background: url('https://zhongyi-shop.oss-cn-beijing.aliyuncs.com/songhe/weixin/static/convenienceService/detailsBg.png')0 0 no-repeat;
		background-size: 752rpx 510rpx;
		position: relative;

		/deep/ .hx-navbar__fixed {
			background: url('https://zhongyi-shop.oss-cn-beijing.aliyuncs.com/songhe/weixin/static/convenienceService/detailsBg.png')0 0 no-repeat;
			background-size: 752rpx 510rpx;
		}

		.dataCls {
			display: flex;
			justify-content: space-between;
			padding: 0 60rpx;
			margin-top: 80rpx;

			.dataCls_l {
				font-size: 28rpx;
				color: #F5F5F5;
				line-height: 40rpx;

				.dataCls_l_num {
					font-size: 38rpx;
				}
			}

			.dataCls_r {
				display: flex;
				align-items: center;

				view {
					font-size: 28rpx;
					color: #0B844A;
					padding: 20rpx 30rpx;
					border-radius: 40rpx;
					background: #ffffff;
					margin-bottom: 20rpx;
				}
			}
		}

		.shadeBox {
			width: 100%;
			height: 40rpx;
			background: #ffffff;
			border-radius: 40rpx 40rpx 0 0;
			position: absolute;
			bottom: 0;
			left: 0;
		}
	}

	.listBox {
		padding: 0 60rpx;

		.listBox_title {
			display: flex;
			justify-content: center;
			padding-bottom: 30rpx;

			>view {
				display: flex;
				align-items: center;
			}

			.listBox_title_text {
				font-size: 30rpx;
				color: #0D8748;
				font-weight: 700;
				margin: 0 30rpx;
				letter-spacing: 2rpx;
			}

			.listBox_line {
				width: 60rpx;
				height: 2rpx;
				background: linear-gradient(90deg, #ffffff, #0d8748);
				border-radius: 1rpx;
			}

			.listBox_line2 {
				width: 60rpx;
				height: 2rpx;
				background: linear-gradient(270deg, #ffffff, #0d8748);
				border-radius: 1rpx;
			}

			.listBox_circle {
				width: 6rpx;
				height: 6rpx;
				background: #0d8748;
				border-radius: 50%;
			}
		}

		.list_item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #E6E6E6;
			padding: 30rpx 0;

			.list_item_l {
				flex: 1;
				.list_item_l_text {
					font-size: 28rpx;
					color: #1A1A1A;
				}

				.list_item_l_time {
					font-size: 24rpx;
					color: #999999;
					margin-top: 10rpx;
				}
			}

			.list_item_r {
				font-size: 30rpx;
			}
		}

		.loadmoreBox {
			display: flex;
			justify-content: center;
			padding: 30rpx;
		}
	}

	.popBox {
		width: 490rpx;

		.popBox_title {
			background: #E6E6E6;
			font-size: 30rpx;
			color: #1A1A1A;
			padding: 25rpx 0;
			font-weight: 700;
			text-align: center;
		}

		.popBox_center {
			padding: 70rpx 27rpx;

			.popBox_inpBox {
				display: flex;
				text-align: center;
				.popBox_inp {
					border-left: 1rpx solid #cccccc;
					border-top: 1rpx solid #cccccc;
					border-bottom: 1rpx solid #cccccc;
					border-radius: 10rpx 0 0 10rpx;
					padding: 10rpx 0;
				}

				.popBox_inp_all {
					display: flex;
					align-items: center;
					background: #20A634;
					font-size: 28rpx;
					color: #FFFFFF;
					padding: 10rpx 30rpx;
					flex-shrink: 0;
					border-radius: 0rpx 10rpx 10rpx 0rpx;
				}
			}

			.popBox_tig {
				font-size: 26rpx;
				color: #999999;
				letter-spacing: 2rpx;
				margin-top: 25rpx;
			}
		}

		.popBox_btnBox {
			display: flex;
			justify-content: space-between;
			margin-top: 37rpx;

			>view {
				width: 200rpx;
				height: 80rpx;
				border: 1rpx solid #20a634;
				border-radius: 41rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 32rpx;
				letter-spacing: 2rpx;
			}

			.popBox_btnBox_close {
				color: #20A634;
			}

			.popBox_btnBox_confirm {
				background: #20A634;
				color: #ffffff;
			}
		}
	}

	.c_red {
		color: #FF3B3B !important;
	}

	.c_green {
		color: #118D44;
	}
</style>
